package com.hiep.connect.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.hiep.connect.client.event.UserLoginEvent;
import com.hiep.connect.client.event.UserUpdateEvent;
import com.hiep.connect.shared.MyUser;

public class EditProfilePresenter implements Presenter {

	public interface EditProfileDisplay {
		HasClickHandlers getSaveButton();

		HasClickHandlers getCancelButton();

		void saveToModel();

		Widget asWidget();
	}

	private EditProfileDisplay view;
	private MyUser user;
	private HandlerManager eventBus;

	public EditProfilePresenter(MyUser user, HandlerManager eventBus,
			EditProfileDisplay view) {
		this.user = user;
		this.eventBus = eventBus;
		this.view = view;

		bind();
	}

	private void bind() {
		// TODO Auto-generated method stub
		this.view.getSaveButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				view.saveToModel();
				eventBus.fireEvent(new UserUpdateEvent(user));
			}
		});

		this.view.getCancelButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new UserLoginEvent(user));
			}
		});
	}

	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub
		container.clear();
		container.add(view.asWidget());
	}

}
